<?php

namespace App\Admin\Views\SwtOrder;

use Abovesky\DcatAdmin\MediaPlayer\Render\VideoLazyRender;
use App\Enums\OrderEnum;
use App\Models\SwtOrder;
use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Tab;
use Throwable;

class OrderGoodsView extends LazyRenderable
{

    /**
     * Get the evaluated contents of the object.
     *
     * @return array|string
     * @throws Throwable
     */
    public function render()
    {
        $order_info = SwtOrder::with(['bj_open','machine','user'])->where('id', $this->payload['id']) -> first();

        $order_info -> order_state_desc = '待支付';

        if($order_info -> need_further_pay == 1) {
            $order_info -> order_state_desc = '部分支付';
        }

        if($order_info -> is_pay == 1){
            $order_info -> order_state_desc = '已支付';
        }

        if($order_info -> refund_amount > 0){
            $order_info -> order_state_desc = '部分退款';
        }

        if($order_info -> is_refund == 1){
            $order_info -> order_state_desc = '已退款';
        }

        if($order_info -> state == OrderEnum::ORDER_REVOKED){
            $order_info -> order_state_desc = '已取消';
        }

        if($order_info -> is_warning == 1){
            $order_info -> order_state_desc = '异常订单';
        }

        $order_info -> order_amount /= 100;
        $order_info -> refund_amount /= 100;
        $order_info -> pay_amount /= 100;

        foreach($order_info->goods as &$goods){
            $goods->goods_price /= 100;
        }

        $video_card = "订单详情";

        if($order_info -> machine -> source_type == 2) {
            $tab = Tab::make();

            $tab -> add('主摄', VideoLazyRender::make(['url' => $order_info->bj_open->video_main_url]));

            $tab -> add('副摄', VideoLazyRender::make(['url' => $order_info->bj_open->video_auxiliary_url]));

            $video_card = $tab->render();
        }

        return Card::make($video_card,admin_view('admin.order.goods', ['order_info' => $order_info]));
    }
}

